import uvicorn
from fastapi import FastAPI, Depends
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html
from starlette.staticfiles import StaticFiles

from dependencies import get_query_token,get_token_header
# 这时我们导入的是模块而不是router对象是防止名称冲突
from routers import items, users
from internal import admin

# 禁用掉框架自带的/docs和/redoc
app = FastAPI(docs_url=None, redoc_url=None,dependencies=[Depends(get_query_token)])
# 静态文件挂载（路由与静态文件映射）
app.mount("/static", StaticFiles(directory="statics"), name="static")

# 使用 app.include_router()，我们可以将每个 APIRouter 添加到主 FastAPI 应用程序中
app.include_router(users.router)
app.include_router(items.router)
app.include_router(
    admin.router,
    prefix="/admin",    # 访问时的路由前缀是/admin，所以最终路由是/admin+admin.py中声明的路径
    tags=["管理员接口"],
    dependencies=[Depends(get_token_header)],
    responses={418: {"description": "admin api"}}
)

# 重新提供一个Swagger UI，相当于我们自己重写了这么一个路由
@app.get("/docs", include_in_schema=False)
def custom_swagger_ui():
    return get_swagger_ui_html(
        openapi_url = "/openapi.json",
        title = "Swagger UI for FastAPI",
        swagger_js_url="/static/swagger-ui-bundle.min.js",
        swagger_css_url="/static/swagger-ui.min.css"
    )

# 这里与Swagger UI一样，重新提供Redoc
@app.get("/redoc", include_in_schema=False)
def custom_redoc():
    return get_redoc_html(
        openapi_url = "/openapi.json",
        title = "Redoc for FastAPI",
        redoc_js_url="/static/redoc.standalone.js"
    )

@app.get("/")
async def root():
    return {"message": "Hello My APP"}

if __name__ == '__main__':
    app.debug = True
    uvicorn.run("main:app", host="127.0.0.1", port=8030, reload=True)
